const config = require('./config');
const MongoClient = require('mongodb').MongoClient;
const sd = require('silly-datetime')
const util = require('util')
const requests = require('request')

function insertDB(datas) {
    MongoClient.connect(config.mongodbUrl, function (err, db) {
        if (err) throw err;
        var dbase = db.db(config.dbName);
        // dbase.createCollection('cnblogs', function (err, res) {
        //     if (err) throw err;
        //     console.log("创建集合!");
        //     db.close();
        // });
        // var myobj = { name: "菜鸟教程", url: "www.runoob" };
        datas.forEach(data => {
            dbase.collection(config.siteName).insertOne(data, function(err) {
                if (err) throw err;
                // console.log("文档插入成功");
            });
        });
        db.close();
    });
}

function insertMysql(conn, datas) {
    datas.forEach(data => {
        var dataList = [];
        dataList.push(data.title);
        dataList.push(data.href);
        dataList.push(data.content);
        dataList.push(sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'));
        var addSql = 'INSERT INTO cnblogs_woodk(article_title,web_url,article_text,create_time) VALUES(?,?,?,?)';
        var addSqlParams = dataList
        conn.query(addSql,addSqlParams,function (err) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
            }else {
                console.log('insert suss!');
            }    
        });
    });
}

// const account = "EALJYLJf1w12grTA"
// const accountKey = "guixuqi7310"
// const num = "10"
// const city = "all"
// const dby_url = `http://httpbapi.dobel.cn/User/getIp&account=${account}&accountKey=${accountKey}&num=${num}&cityId=${city}`

// var params = {
//     "account": account,
//     "accountKey": accountKey,
//     "num": num,
//     "cityId": city
// }

// pro_ip = resp["data"][0]["ip"]
// pro_port = resp["data"][0]["port"]
// ip = '{}:{}'.format(pro_ip, pro_port)
// proxies = {
//     "http": "http://" + ip,
// }

// proxy
// async function getProxy(loop = 2) {
//     return new Promise((resolve) => {
//         requests({
//             method: 'GET',
//             url: dby_url,
//             headers: {
//                 // 'util-app': 'tortious/ua5oSsl4vCEDxGVFA2BLrMctQ3zHXhZq', 
//                 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
//             }
//         }, async(error, response, body) => {
//             try {
//                 if(!error && response.statusCode == 200) {
//                     let data = JSON.parse(body).data;
//                     // let proxy_ip = data[0].ip;
//                     // let proxy_port = data[0].port;
//                     // let proxy = util.format('http://%s:%s', proxy_ip, proxy_port);  
//                     // let proxy = {
//                     //     ip: proxy_ip,
//                     //     port: proxy_port
//                     // }
//                     // console.log(proxy);
//                     resolve(data);
                    
//                 }else {
//                     throw new Error('proxy of undefined');
//                 }
//             } catch (error) {
//                 if (loop > 0) {
//                     return await getProxy(--loop);
//                 }else {
//                     console.log('getProxy fail: ', error.message)
//                 }
//             }
            
//         })
//     })
// }


// proxy
async function getProxy(loop = 5) {
    return new Promise((resolve) => {
        requests({
            method: 'GET',
            url: 'http://172.20.0.160:8090/util/proxy-invoke?site=tortious&min_exp=30&min_use=2',
            headers: {
                'util-app': 'tortious/ua5oSsl4vCEDxGVFA2BLrMctQ3zHXhZq', 
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
            }
        }, async(error, response, body) => {
            try {
                if(!error && response.statusCode == 200) {
                    let data = JSON.parse(body).data;
                    let proxy = []
                    proxy.push(data)
                    resolve(proxy);
                }else {
                    throw new Error('proxy of undefined');
                }
            } catch (error) {
                if (loop > 0) {
                    return await getProxy(--loop);
                }else {
                    console.log('getProxy fail: ', error.message)
                }
            }
        })
    })
}

module.exports = { 
    insertMysql,
    insertDB,
    getProxy
}

